package com.iyao_app.util;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.iyao_app.R;


/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/14.
 */

public class CustomDialog_ extends Dialog {

    private final TextView mTvContent;
    private final TextView mTvConfirm;
    private final TextView mTvCancel;
    private boolean isDismiss = true;

    public CustomDialog_(@NonNull Context context) {
        super(context, R.style.MyDialog);
        setContentView(R.layout.dialog_custom_);
        mTvContent = findViewById(R.id.tv_content);
        mTvConfirm = findViewById(R.id.tv_confirm);
        mTvCancel = findViewById(R.id.tv_cancel);
        Window window = getWindow();
        //设置动画效果
        window.setWindowAnimations(R.style.MyDialogAnim);

    }

    public CustomDialog_ setContent(String content) {
        if (mTvContent != null) {
            mTvContent.setText(content);
        }
        return this;
    }

    public CustomDialog_ setConfirmText(String str) {
        if (mTvConfirm != null) {
            mTvConfirm.setText(str);
        }
        return this;
    }

    public CustomDialog_ setCancelText(String str) {
        if (mTvCancel != null) {
            mTvCancel.setText(str);
        }
        return this;
    }

    public CustomDialog_ setOnConfirmClickListener(final View.OnClickListener listener) {
        mTvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onClick(v);
                if (isDismiss) {
                    CustomDialog_.this.dismiss();
                }
            }
        });

        return this;
    }

    public CustomDialog_ setOnCancelClickListener(final View.OnClickListener listener) {
        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onClick(v);
                if (isDismiss) {
                    CustomDialog_.this.dismiss();
                }
            }
        });

        return this;
    }


    public CustomDialog_ setConfirmDismiss(boolean isDismiss) {
        this.isDismiss = isDismiss;
        return this;
    }

    public CustomDialog_ setCanceled(boolean isCancel) {
        this.setCanceledOnTouchOutside(isCancel);
        return this;
    }
}
